package com.bjx.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.common.security.domain.UserPostVO;
import com.bjx.system.domain.entity.UserPostEntity;
import com.bjx.system.mapper.UserPostMapper;
import com.bjx.system.service.IUserPostService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 用户职位关联服务实现
 * @author: liushuai
 * @date: 2022/10/15
 */
@Service
public class UserPostServiceImpl extends ServiceImpl<UserPostMapper, UserPostEntity> implements IUserPostService {
    @Resource
    private UserPostMapper userPostMapper;


    @Override
    public List<UserPostVO> listUserPost(Long userId) {
        LambdaQueryWrapper<UserPostEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPostEntity::getUserId,userId);
        List<UserPostEntity> userPostEntities = userPostMapper.selectList(wrapper);
        List<UserPostVO> list = Convert.toList(UserPostVO.class, userPostEntities);
        return list;
    }

    @Override
    public void saveUserPostBatch(List<UserPostVO> vos, Long userId) {
        LambdaQueryWrapper<UserPostEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPostEntity::getUserId,userId);
        userPostMapper.delete(wrapper);
        if(CollectionUtil.isNotEmpty(vos)){
            List<UserPostEntity> userPostEntities = new ArrayList<>();
            vos.stream().forEach(res->{
                UserPostEntity entity = Convert.convert(UserPostEntity.class, res);
                entity.setUserId(userId);
                userPostEntities.add(entity);
            });
            this.saveBatch(userPostEntities);
        }
    }
}
